/*
*  Copyright 2019-2023 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.webuser.mapper;

import me.zhengjie.webuser.domain.WebUsers;
import me.zhengjie.webuser.domain.vo.WebUsersAvatarInfo;
import me.zhengjie.webuser.domain.vo.WebUsersQueryCriteria;
import java.util.List;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

/**
* @author meorin
* @date 2024-12-07
**/
@Mapper
public interface WebUsersMapper extends BaseMapper<WebUsers> {

    IPage<WebUsers> findAll(@Param("criteria") WebUsersQueryCriteria criteria, Page<Object> page);

    List<WebUsers> findAll(@Param("criteria") WebUsersQueryCriteria criteria);

    WebUsers findByEmail(@Param("email") String email);

    WebUsers findByUsername(@Param("username") String username);

    WebUsers findByEmailOrUsername(@Param("emailOrUsername") String emailOrUsername);

    //WebUsers updateAvatar(@Param("avatarUrl") String avatarUrl);

    @Update("UPDATE web_users SET bio = #{bio} WHERE id = #{userId}")
    void updateBio(@Param("userId") Integer userId, @Param("bio") String bio);

    @Update("UPDATE web_users SET email = #{email} WHERE id = #{userId}")
    void updateEmail(@Param("userId") Integer userId, @Param("email") String email);

    @Update("UPDATE web_users SET avatar_url = #{avatarUrl} WHERE id = #{userId}")
    void updateAvatar(@Param("userId") Integer userId, @Param("avatarUrl") String avatarUrl);

    @Select("SELECT avatar_url FROM web_users WHERE id = #{userId}")
    String getAvatarUrlById(Integer userId);

    @Select("SELECT avatar_url,id FROM web_users WHERE username = #{username}")
    WebUsersAvatarInfo getAvatarUrlByUsername(String username);

    @Update("UPDATE web_users SET score = score + #{points} WHERE id = #{userId}")
    void incrementScore(@Param("userId") Integer userId, @Param("points") Integer points);

}